package com.sky.controller.User;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车")
public class ShoppingCartController {
    @Autowired
    ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        //1.接受请求参数：json ，实体对象
        log.info("添加购物车：{}", shoppingCartDTO);
        //2.调用业务层，实现添加购物车
        shoppingCartService.add(shoppingCartDTO);//后绪步骤实现
        //3.封装返回处理结果
        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list() {

        List<ShoppingCart> shoppingCartList = shoppingCartService.shoppingCart();

        return Result.success(shoppingCartList);
    }

    /*
     * 清空购物车
     * */
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.clean();
        return Result.success();
    }

    /*
     * 删除购物车中一个商品
     * */
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO) {

        shoppingCartService.delete(shoppingCartDTO);

        return Result.success();
    }
}
